package cn.ljy.uem.service.impl;

import cn.ljy.authority.model.SysDepartment;
import cn.ljy.authority.model.SysRole;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.uem.dao.SchoolDepartmentMapper;
import cn.ljy.uem.dao.SchoolMajorGroupMapper;
import cn.ljy.uem.dao.SchoolMajorGroupTeacherMapper;
import cn.ljy.uem.dao.SchoolTeacherMapper;
import cn.ljy.uem.model.SchoolDepartment;
import cn.ljy.uem.model.SchoolMajorGroup;
import cn.ljy.uem.model.SchoolTeacher;
import cn.ljy.uem.model.query.SchoolMajorGroupQuery;
import cn.ljy.uem.service.SchoolMajorGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @description 专业群的业务层接口
 * @author 王秋林
 * @date 2022-7-18 16:00
 */
@Service
@Slf4j
public class ISchoolMajorGroupService extends IBaseService<SchoolMajorGroup, SchoolMajorGroupQuery> implements SchoolMajorGroupService {

    @Autowired
    private SchoolMajorGroupMapper mapper;

    @Autowired
    private SchoolDepartmentMapper schoolDepartmentMapper;

    @Autowired
    private SchoolTeacherMapper schoolTeacherMapper;
    @Autowired
    private SchoolMajorGroupTeacherMapper schoolMajorGroupTeacherMapper;
    @Override
    protected void beforeGet(Serializable id) {
        if(mapper.findById(id)==null){
            throw new MyWebException("查找失败：找不到指定记录");
        }
    }

    @Override
    protected void beforeUpdate(SchoolMajorGroup schoolMajorGroup) throws MyWebException {
        if(schoolMajorGroup.getId()==null){
            throw new MyWebException("编辑失败：id不能为空");
        }
        if(mapper.findById(schoolMajorGroup.getId())==null){
            throw new MyWebException("编辑失败：找不到指定记录");
        }
        //判断专业群添加部门是否存在
        if(schoolDepartmentMapper.findById(schoolMajorGroup.getDepartmentId())==null){
            throw new MyWebException("添加失败：该部门不存在");
        }
        /**
         * 重新处理专业群和群主的关系。
         * 考虑到关系表没有自增id，且专业群和群主关系的数量不会多，本来可以简化逻辑：先全部删除该专业群所有群主关系，再重新添加
         * 但为了保证专业群和群主关系的建立时间可查，故采用规范的判断
         */
        //将前端传递过来的集合转换为set集合,顺便去重
        Set<Long> teacherIdsSet = new HashSet<Long>(Arrays.asList(schoolMajorGroup.getTeacherIds()));
        //获取所有的群主
        List<SchoolTeacher> teachersOfAll=schoolTeacherMapper.findAll(null);
        //获取原来的群主
        List<SchoolTeacher> teachersOrigin=schoolMajorGroupTeacherMapper.findAllTeacherByMajorGroupId(schoolMajorGroup.getId());
        //处理专业群和群主的关系
        for(SchoolTeacher schoolTeacher:teachersOfAll){
            if(teacherIdsSet.contains(schoolTeacher.getId())){//如果前端勾选了该群主
                if(!teachersOrigin.contains(schoolTeacher)){//如果该群主原来不存在
                    //添加该群主关系
                    schoolMajorGroupTeacherMapper.save(schoolMajorGroup.getId(),schoolTeacher.getId(),1);
                }
            }else{//如果没有勾选该群主
                if(teachersOrigin.contains(schoolTeacher)) {//如果该群主原来存在
                    //移除该群主关系
                    schoolMajorGroupTeacherMapper.delete(schoolMajorGroup.getId(),schoolTeacher.getId());
                }
            }
        }

    }

    @Override
    protected void beforeSave(SchoolMajorGroup schoolMajorGroup) {
        if (schoolMajorGroup.getDepartmentId() == null){//判断专业群是否存在对应部门不为空
            throw new MyWebException("添加失败：找不到指定记录");
        }
        //判断专业群是否存在
        if(mapper.countByName(schoolMajorGroup.getName())>0){
            throw new MyWebException("添加失败：该专业群已存在");
        }
        //判断专业群添加部门是否存在
        if(schoolDepartmentMapper.findById(schoolMajorGroup.getDepartmentId())==null){
            throw new MyWebException("添加失败：该部门不存在");
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        schoolMajorGroupTeacherMapper.deleteByMajorGroupIds(ids);
    }


    @Override
    protected void afterGet(SchoolMajorGroup schoolMajorGroup) {
    }

    @Override
    protected void afterFindAll(List<SchoolMajorGroup> list) {
    }

    @Override
    protected void afterSave(SchoolMajorGroup schoolMajorGroup) {
        if (schoolMajorGroup.getTeacherIds() != null){
            for(Long teacherId:schoolMajorGroup.getTeacherIds()){
                schoolMajorGroupTeacherMapper.save(schoolMajorGroup.getId(),teacherId,1);
            }
        }
    }
}
